package com.zeny.online.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * @ClassName WSServerInitalzer
 * @Description TODO
 * @Author zeny
 */
public class WSServerInitalzer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast(new HttpServerCodec());
        //大数据流
        pipeline.addLast(new ChunkedWriteHandler());
        pipeline.addLast(new HttpObjectAggregator(1024 * 64));
        //----------------------以上是用于支持http协议----------------------

        //----------------------  增加心跳支持start  ----------------------
        //针对客户端,如果在一分钟时没有向服务端发送读写心跳（all）, 则主动断开;读空闲或写空闲不处理
        pipeline.addLast(new IdleStateHandler(5,10,20));
        //自定义空闲检测
        pipeline.addLast(new HeartHandler());
        //----------------------  增加心跳支持end  ------------------------
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
        pipeline.addLast(new ChatHandler());

    }
}
